import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:testlzhu/view/item_rent.dart';

typedef ItemWidgetBuilder = Widget Function(BuildContext context, int index);
typedef MyRefreshController = void Function(RefreshController controller);
typedef ItemCount = int Function(BuildContext context);


class MySmartRefresher extends StatefulWidget {
  ItemWidgetBuilder itemBuilder;

  MyRefreshController myRefreshController;

  ItemCount itemCount;

  final VoidCallback? monRefresh;
  final VoidCallback? loading;




  MySmartRefresher({Key? key, required this.myRefreshController, required this.itemBuilder, required this.itemCount, this.monRefresh, this.loading}) : super(key: key);

  @override
  _MySmartRefresherState createState() => _MySmartRefresherState();
}

class _MySmartRefresherState extends State<MySmartRefresher> {
  final RefreshController _refreshController = RefreshController(initialRefresh: false);

  @override
  void initState() {
    super.initState();
    widget.myRefreshController(_refreshController);
  }

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      enablePullUp: true,
      enablePullDown: true,
      header: const WaterDropHeader(),
      controller: _refreshController,
      footer: const ClassicFooter(),
      onRefresh: widget.monRefresh,
      onLoading: widget.loading,
      child: ListView.separated(
        itemCount: widget.itemCount(context),
        itemBuilder: (context, index) {
          return widget.itemBuilder(context, index);
        },
        separatorBuilder: (BuildContext context, int index) {
          return const Divider();
        },
      ),
    );
  }
}
